<?php

class Controller_PersonInfo extends Controller_Website {
    
    public function action_index() {
        $id = Arr::get($_GET, 'id');
        $m_personInfo = Model::factory('personInfo');
        $ret = $m_personInfo->getRow($where=array('uid'=>$id));
//         var_dump($list);exit;
        $m_personCategory = Model::factory('personCategory');
        $personCategory = $m_personCategory->getAll()->as_array();
        

        $this->content = View::factory('personInfo/index');
        $this->content->ret = $ret;
        $this->content->personCategory = $personCategory;
    }

    public function action_list() {
        
        
        $where = array();
        $status = Arr::get($_GET, 'status');
        if (!empty($status)){
            $where['status'] = $status;
        }
        
        $cid = Arr::get($_GET, 'cid');
        if (!empty($cid)){
            $where['cid'] = array('FIND_IN_SET'=>$cid);
        }
        $where['ORDER'] = 'id DESC';
        $where['user_type'] = 1;
//         var_dump($where);
//         $where = array_filter($where, 'strlen');
        
        $name = Arr::get($_GET, 'name');
        if (!empty($name)) {
            $where['name'] = array('like'=>"%$name%");
        }
        
        $m_personInfo = Model::factory('personInfo');
        $total = $m_personInfo->count($where);
        $pager = new Pager($total, 5);
        $list = $m_personInfo->select($pager->offset, $pager->size, $where)->as_array();
        
        $m_personCategory = Model::factory('personCategory');
        $cat_list = $m_personCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);

        $personCategory = $m_personCategory->getAll(array('status'=>'open'))->as_array();
        
        $m_check = Model::factory('Check');
        $check = $m_check->getAll()->as_array();
        
        foreach ($list as &$item){
            if($item['cid'] == 0){
                $item['price'] ="0";
                $item['person_cate_name'] = "该用户信息未完全";
            }else{
                $item['cid'] = explode(',', $item['cid']);
                $where = array(
                    'uid' => $item['uid'],
                    'ORDER' => 'price DESC'
                );
                
                $job = Model::factory('jobType');   //将获取到的类型ID 进行筛选
                $userJob = $job->getAll($where)->as_array();

                $item['price'] = $userJob[0]['price'];

                foreach ($personCategory as $item2){
                    for ($i=0; $i<count($item['cid']); $i++) {
                        if ($item2['id'] == $item['cid'][$i]){
                            $item['person_cate_name'][] = $item2['name'];
                        }
                    }

                }
            }
            foreach ($check as $item3) {
                if ($item['uid'] == $item3['user_id']) {
                    $item['is_check_info'] = $item3['is_check_info'];
                }
            }
        }
        
        $m_works = Model::factory('works');
        $works = $m_works->getAll()->as_array();

        $this->content = View::factory('personInfo/list');
        $this->content->list = $list;
        $this->content->pager = $pager;
        $this->content->works = $works;
        $this->content->personCategory = $personCategory;
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->info = null;
    }

    public function action_add() {
        $m_personInfo = Model::factory('personInfo');
        
        if (!empty($_POST)) {
            $data = $_POST;
            unset($_POST['works']);
//             var_dump($data);exit;
            if (!empty($data['pic'])) {
                $data['pic'] = array_pop($data['pic']);
            }
            $data['add_time'] = time();
            $data['edit_time'] = strtotime('now');
//             var_dump($data);exit;
            $ret = $m_personInfo->insert($data);
            if ($ret !== false) {
                $this->redirect('personInfo/list');
            }
        }
        
        $m_personCategory = Model::factory('personCategory');
        $cat_list = $m_personCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $m_professionCategory = Model::factory('professionCategory');
        $cat_list2 = $m_professionCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree2 = Category::get_children_tree($cat_list2);
        
        $m_projectCategory = Model::factory('projectCategory');
        $cat_list3 = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree3 = Category::get_children_tree($cat_list3);
        
        $m_city = Model::factory('city');
        $m_allCity = Model::factory('allCity');
        $city = $m_allCity::index();
//         var_dump($city);exit;
        $this->content = View::factory('personInfo/add');
        $this->content->info = null;
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->cat_list2 = $cat_list2;
        $this->content->cat_tree2 = $cat_tree2;
        $this->content->cat_list3 = $cat_list3;
        $this->content->cat_tree3 = $cat_tree3;
        $this->content->city = $city;
    }

    public function action_edit() {
        $id = Arr::get($_GET, 'id');
        $m_personInfo = Model::factory('personInfo');
        $info = $m_personInfo->getRowById($id);
        
        
        if (!empty($_POST)) {
            
            $data = $_POST;
            unset($_POST['works']);
//             var_dump($_POST);exit;
            if (!empty($data['pic'])) {
                $data['pic'] = array_pop($data['pic']);
            }
            $data['edit_time'] = strtotime('now');
            $ret = $m_personInfo->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('personInfo/list');
            }
        }
        
        $m_personCategory = Model::factory('personCategory');
        $cat_list = $m_personCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $m_professionCategory = Model::factory('professionCategory');
        $cat_list2 = $m_professionCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree2 = Category::get_children_tree($cat_list2);
        
        $m_projectCategory = Model::factory('projectCategory');
        $cat_list3 = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree3 = Category::get_children_tree($cat_list3);
        
        $m_city = Model::factory('city');
        $m_allCity = Model::factory('allCity');
        $city = $m_allCity::index();
        
        $this->content = View::factory('personInfo/edit');
//         $this->content->c_device = $c_device;
        $this->content->info = $info;
        
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->cat_list2 = $cat_list2;
        $this->content->cat_tree2 = $cat_tree2;
        $this->content->cat_list3 = $cat_list3;
        $this->content->cat_tree3 = $cat_tree3;
        $this->content->city = $city;
    }

    public function action_del() {
        $id = Arr::get($_GET, 'id');
        $m_personInfo = Model::factory('personInfo');
        $ret = $m_personInfo->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    public function action_close() {
        $id = Arr::get($_GET, 'id');
        $data = array(
            'status' => 2,
        );
        $m_personInfo = Model::factory('personInfo');
        $ret = $m_personInfo->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    } 
    
    public function action_open() {
        $id = Arr::get($_GET, 'id');
        $data = array(
            'status' => 1,
        );
        $m_personInfo = Model::factory('personInfo');
        $ret = $m_personInfo->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    public function action_addworks() {
        $m_works = Model::factory('works');
        $personinfo_id = Arr::get($_GET, 'id');
        $m_personInfo = Model::factory('personInfo');
        $personInfo = $m_personInfo->getRow(array('id'=>$personinfo_id),'uid');
        
        if (!empty($_POST)) {
            $data = $_POST;
            if (!empty($data['pic'])) {
                //拥有多张图片 ---- 存放到图片库里
                $pic_arr = $data['pic'];
                $pic_ids= array();
                $m_pic = Model::factory('pic');
                foreach ($pic_arr as $val){
                    $res = $m_pic->insert(array('picurl'=>$val));
                    $pic_ids[]= $res[0]; 
                }
                
                $data['pic_item'] = json_encode($pic_ids);
                unset($data['pic']);
                unset($data['picurl']);    
            }
            $data['user_id'] = $personInfo['uid'];
            $data['create_time'] = time();
            $data['update_time'] = strtotime('now');
            //             var_dump($data);exit;
            $ret = $m_works->insert($data);
            if ($ret !== false) {
                $this->redirect('personInfo/list');
            }
        }
        
        
        $m_projectCategory = Model::factory('projectCategory');
        $cat_list = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $m_personCategory = Model::factory('personCategory');
        $cat_list2 = $m_personCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree2 = Category::get_children_tree($cat_list2);
    
        $this->content = View::factory('personInfo/addworks');
        $this->content->info = null;
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->cat_list2 = $cat_list2;
        $this->content->cat_tree2 = $cat_tree2;
        
    }

    public function action_details()
    {
        $id = Arr::get($_GET, 'id');
        
        $m_personInfo = Model::factory('personInfo');
        $personInfo = $m_personInfo->getRow(array('id'=>$id));

        $cid = $personInfo['cid'];
        $cid = explode(',',$cid);
        $price = [];
        foreach ($cid as $c)
        {
            $model = Model::factory('jobType');
            $model = $model->getRow(array(
                'uid'=>$personInfo['uid'],
                'cid'=>$c
            ));
            if($model['cid'] == 7){
                $daoyan = Model::factory('jobService');
                $daoyan = $daoyan->getAll(array(
                    'uid'=>$personInfo['uid'],
                    'ORDER'=>'price DESC'
                ))->as_array();
                $model['price'] = $daoyan[0]['price'];
            }
            $typeName = Model::factory('personCategory');
            $typeName = $typeName->getRow(array('id'=>$c));
            $model['typename'] = $typeName['name'];
            $price[] = $model;
        }
        
        $m_check = Model::factory('Check');
        $check = $m_check->getRow(array('user_id'=>$personInfo['uid']));

        $this->content = View::factory('personInfo/details');
        $this->content->info = $personInfo;
        $this->content->cid = $price;
        $this->content->check = $check;
    }
    
    public function action_check_ok() {
        $id = Arr::get($_GET, 'id');
        $m_personInfo = Model::factory('personInfo');
        $personInfo = $m_personInfo->getRowById($id);
        
        $m_check = Model::factory('Check');
        $data = array(
            'user_id'=>$personInfo['uid'],
            'is_check_info'=>1,
        );
        $ret = $m_check->insert($data);
        if ($ret) {
            $m_creditScore = Model::factory('CreditScore');
            $user_id = $personInfo['uid'];
            $field = 'basic_score';
            $ret2 = $m_creditScore->updateCredit($user_id,$field);
            
            $creditScore = $m_creditScore->getRow(array('user_id'=>$user_id));
            $data2 = array(
                'credit_score'=>$creditScore['total_score'],
            );
            $ret3 = $m_personInfo->updateById($data2, $id);
            if ($ret3) {
                $this->redirect('personInfo/list');
            }
            
        }
    }
    
    public function action_check_no() {
        $id = Arr::get($_GET, 'id');
        $this->redirect('personInfo/list');
    }
}

